﻿<?php
ini_set('date.timezone','Asia/Shanghai');
include 'simple_html_dom.php';
include 'config_catchanzhi.php';

define('APPS_NUM',2405);
define('GAMES_NUM',977);

//define('DBPAS',"coolchuan2012");
define('DBPAS',"123456");

//coolchuan2012
$link = mysql_connect("localhost","root",DBPAS); 
mysql_select_db("catchappdataall", $link);
mysql_query('SET NAMES UTF8');


define('ONCECATCHCOUNT',15);

define('ONCECATCHCOUNTMAX',40);

define('WEBURLHEADER','http://m.anzhi.com');

//define('LOGPATHHEADER','/var/www/catchjiankongdata/');
define('LOGPATHHEADER','');




/*

function Post($url, $post = null)
{
	$context = array();
	if (is_array($post))
	{
		ksort($post);
		$context['http'] = array('method' => 'POST',
					             'content' => http_build_query($post, '', '&'),
					             );
	}
	return file_get_contents($url, false, stream_context_create($context));
}
$data = array('SID' => 'f4432c628f4f4122520f33a404e72250');
var_dump(Post('http://118.26.224.21/api.php', $data));


//die();

$param = array('SID'=>'f4432c628f4f4122520f33a404e72250');  
$ctx = stream_context_create(array(  
'http' => array(  
'timeout' => 10,
'method' => 'POST',
'content' => http_build_query($param, '', '&'),  )
  )  );
  
@$json_string = file_get_contents("http://118.26.224.21/api.php", 0, $ctx); 
$obj=json_decode($json_string);

var_dump($json_string);

die();
*/

getinfoandrank();

//getcomment();

function getinfoandrank()
{
	//获取已存在app基础数据
	
	//应用分类
	$apptype_arr = getapptypelist();
	foreach ($apptype_arr as $apptype)
	{
		//每一类的url
		$apps_url_app_type_list = $apptype['url'];
		decodeurlandgetdata($apptype['id'],"soft",$apps_url_app_type_list);
	}
	//应用总榜
//	decodeurlandgetdata(101,"soft","http://apps.wandoujia.com/api/v1/feeds?f=phoenix2&v=3.24.1&vc=3918&u=b19e8780415b44108c0c7f4014898b7b938fd64b&ch=wandoujia_wap&opt_fields=adsCursor,data.app.apks.compatible,data.app.apks.incompatibleDetail,data.reason.*,data.app.title,data.app.packageName,data.app.ad,data.app.icons.px78,data.app.installedCountStr,data.app.apks.downloadUrl.url,data.app.apks.bytes,data.app.apks.verified,data.app.apks.versionName,data.app.apks.versionCode,data.app.detailParam,data.app.imprUrl,data.app.stat.weeklyStr,data.app.exclusiveBadge,data.app.apks.md5,data.app.editorComment,data.app.award.issue,data.app.apks.superior,data.app.apks.paidType,data.app.likesRate&area=index&ads_start=9");
	
	
	//游戏分类
	$gametype_arr = getgametypelist();
	foreach ($gametype_arr as $gametype)
	{
		//每一类的url
		$apps_url_game_type_list = $gametype['url'];
		decodeurlandgetdata($apptype['id'],"game",$apps_url_game_type_list);
	}
	//游戏总榜
//	decodeurlandgetdata(102,"game","http://apps.wandoujia.com/api/v1/feeds?f=phoenix2&v=3.24.1&vc=3918&u=b19e8780415b44108c0c7f4014898b7b938fd64b&ch=wandoujia_wap&opt_fields=adsCursor,data.app.apks.compatible,data.app.apks.incompatibleDetail,data.reason.*,data.app.title,data.app.packageName,data.app.ad,data.app.icons.px78,data.app.installedCountStr,data.app.apks.downloadUrl.url,data.app.apks.bytes,data.app.apks.verified,data.app.apks.versionName,data.app.apks.versionCode,data.app.detailParam,data.app.imprUrl,data.app.stat.weeklyStr,data.app.exclusiveBadge,data.app.apks.md5,data.app.editorComment,data.app.award.issue,data.app.apks.superior,data.app.apks.paidType,data.app.likesRate&area=game&ads_start=6");

	//豌豆荚排行榜
//	decodeurlandgetdata(111,"paihang","http://apps.wandoujia.com/api/v1/apps?f=phoenix2&v=3.24.1&vc=3918&u=b19e8780415b44108c0c7f4014898b7b938fd64b&ch=wandoujia_wap&opt_fields=apks.compatible,apks.incompatibleDetail,title,packageName,ad,icons.px78,installedCountStr,apks.downloadUrl.url,apks.bytes,apks.verified,apks.versionName,apks.versionCode,detailParam,imprUrl,stat.weeklyStr,exclusiveBadge,apks.md5,editorComment,apks.paidType,likesRate,apks.superior&type=weeklytop");
	
	//设计奖
//	decodeurlandgetdata(112,"sheji","http://apps.wandoujia.com/api/v1/apps?type=award&f=phoenix2&opt_fields=apks.compatible%2Capks.incompatibleDetail%2Ctitle%2CpackageName%2Cad%2Cicons.px78%2CinstalledCountStr%2Capks.downloadUrl.url%2Capks.bytes%2Capks.verified%2Capks.versionName%2Capks.versionCode%2CdetailParam%2CimprUrl%2Caward.blogTitle%2Capks.md5%2CeditorComment%2Capks.paidType%2ClikesRate%2Capks.superior&v=3.24.1&u=b19e8780415b44108c0c7f4014898b7b938fd64b&ch=wandoujia_wap");

}




function decodeurlandgetdata($catchtype,$appbigtype,$urlheader)
{
	$app_exit_infos = get_exit_appinfo_fromdb();
	$app_exit_versions = get_exit_appversion_fromdb();
	
	$app_today_data = get_today_data();
	$app_today_score = get_today_score();
	
	for($i=1;;)
	{
		$apps_url_app_all_list = $urlheader.$i.".html";
		
//		本次为空，结束
//		if((count($appinfoarr) == 0) || ($appinfoarr == ""))
//		{
//			break;
//		}
		
		//有内容，解析
		
		$rankpos = ($i) * ONCECATCHCOUNT;
		$app_info_arr_insertdb_arr = catchinfodata($catchtype,$appbigtype,$rankpos,$apps_url_app_all_list,$i);
		
		
		
		
		$app_info_arr_insertdb['app_id'] = 0;
		$app_info_arr_insertdb['app_version_id'] = 0;
		
		//写数据库
		foreach ($app_info_arr_insertdb_arr as $app_info_arr_insertdb)
		{
			//判断是否需要更新信息库
			$searchinfoid = searchisexit($app_info_arr_insertdb, $app_exit_infos);
			if($searchinfoid == 0)
			{
				$app_info_arr_insertdb['app_id'] = insert_appinfo_ifnotexit_todb($app_info_arr_insertdb);
				$app_info_arr_insertdb['app_version_id'] = insert_appversion_ifnotexit_todb($app_info_arr_insertdb);
				
				//重新获取存在app信息，因为有了更新
				$app_exit_infos = get_exit_appinfo_fromdb();
				$app_exit_versions = get_exit_appversion_fromdb();
			}
			else 
			{
				$app_info_arr_insertdb['app_id'] = $searchinfoid;
				$searchversionid = searchisexitversion($app_info_arr_insertdb, $app_exit_versions);
				if($searchversionid == 0)
				{
					$app_info_arr_insertdb['app_version_id'] = insert_appversion_ifnotexit_todb($app_info_arr_insertdb);
					
					//重新获取存在app信息，因为有了更新
					$app_exit_versions = get_exit_appversion_fromdb();
				}
				else 
				{
					$app_info_arr_insertdb['app_version_id'] = $searchversionid;
				}
			}
			//判断当天没有抓过，再写入
			if(searchisexittodaydata($app_info_arr_insertdb, $app_today_data) == 0)
			{
				update_appinfo_todb($app_info_arr_insertdb);
				$app_today_data = get_today_data();
			}
			if(searchisexittodayscore($app_info_arr_insertdb, $app_today_score) == 0)
			{
				update_appscore_todb($app_info_arr_insertdb);
				$app_today_score = get_today_score();
			}

		}
		
		$i++;
//		$i = $i + ONCECATCHCOUNT;
	}
}

function searchisexit($app_infos, $app_exit_info) 
{
	foreach ($app_exit_info as $app_exit) 
	{
		if ($app_infos['packagename'] == $app_exit["AppPackageName"]) 
		{
			return $app_exit['id'];
		}
	}
	return 0;
}

function searchisexitversion($app_infos, $app_exit_version) 
{
	foreach ($app_exit_version as $app_exit) 
	{
		if (($app_infos['app_id'] == $app_exit["app_id"]) && ($app_infos['Versioncode'] == $app_exit["versionCode"]) ) 
		{
			return $app_exit['id'];
		}
	}
	return 0;
}

function searchisexittodaydata($app_infos, $app_today_data) 
{
	foreach ($app_today_data as $app_exit) 
	{
		if (($app_infos['app_id'] == $app_exit["app_id"]) && ($app_infos['AppRanking'] == $app_exit["rank"]) && ($app_infos['AppRankingType'] == $app_exit["ranktype"]))
		{
			return $app_exit['id'];
		}
	}
	return 0;
}
function searchisexittodayscore($app_infos, $app_today_score) 
{
	foreach ($app_today_score as $app_exit)
	{
		if (($app_infos['app_id'] == $app_exit["app_id"]))
		{
			return $app_exit['id'];
		}
	}
	return 0;
}



//排名分类，app大类，app信息
function catchinfodata($catchtype,$appbigtype,$flag,$apps_url_app_all_list,$pageno)
{
	// 新建一个Dom实例 
	$html = new simple_html_dom();
	// 从url中加载
	$html->load_file($apps_url_app_all_list);
	
	$app_url_arr = array();
	
	$ranking = $flag ;//* ONCECATCHCOUNT;
	$app_info_insertdb_arr = array();
	
	if($pageno == 0)
	{
		$appinfo_arr = $html->find('ul.app-items-list li');
	}
	else 
	{
		$appinfo_arr = $html->find('li');
	}

	foreach($appinfo_arr as $appurl) 
	{
		$ranking++;
		$app_info_insertdb = array();
		
//		$app_info_insertdb['iconurl'] = $appurl->find('div.app_icon a img',0)->src;
//		var_dump($app_info_insertdb);die();
//	    echo WEBURLHEADER.$app_info_insertdb['iconurl'];

		
		$appinfotemp = $appurl->find('div.itmes-icon img',0);
		$app_info_insertdb['AppLogo'] = $appinfotemp->src;
		
		$appinfotemp = $appurl->find('div.items-info',0);
		$app_info_insertdb['name'] = addslashes($appinfotemp->find('h2',0)->plaintext);
		
		$app_info_insertdb['url'] = WEBURLHEADER.$appurl->find('a.clickarea',0)->href;
		
		$appdetail_info_arr = getappdetailinfo($app_info_insertdb['url']);
		
		$app_info_insertdb['AppDownTimes'] = $appdetail_info_arr['AppDownTimes'];
		$app_info_insertdb['AppVersion'] = $appdetail_info_arr['AppVersion'];
		$app_info_insertdb['Versioncode'] = "";
		$app_info_insertdb['AppSize'] = $appdetail_info_arr['AppSize'];
		
		var_dump($app_info_insertdb);die();
		

		$app_info_insertdb['packagename'] = $appinfo_data->packageName;
		$app_info_insertdb['ScoreType'] = $appinfo_data->likesRate;//%的人喜欢
		$app_info_insertdb['Rating'] = @"";//$appinfo->rating;
		
		
		
		
		
		$app_info_insertdb['AppType'] = "";
//		if($catchtype <= 100)
		{
			$app_info_insertdb['AppType'] = $catchtype;
		}
		$app_info_insertdb['AppBigType'] = $appbigtype;
		$app_info_insertdb['AppRanking'] = $ranking;
		$app_info_insertdb['AppRankingType'] = $catchtype;
		
		$app_info_insertdb_arr[] = $app_info_insertdb;
	}
	$html->clear();
	
	return $app_info_insertdb_arr;
}

function getappdetailinfo($app_detail_url)
{echo $app_detail_url;
	$appdetailinfo_arr = array();
	
	// 新建一个Dom实例 
	$html_detail = new simple_html_dom();
	// 从url中加载 
	$html_detail->load_file($app_detail_url);
	$appdetailinfo = $html_detail->find('div.app_header ul li');
	
	$appdetailinfo_arr['AppDownTimes'] = str_replace("下载：大于","",$appdetailinfo[0]->plaintext);
	$appdetailinfo_arr['AppDownTimes'] = str_replace("次","",$appdetailinfo_arr['AppDownTimes']);
	$appdetailinfo_arr['AppDownTimes'] = str_replace(" ","",$appdetailinfo_arr['AppDownTimes']);
	
	$appdetailinfo_arr['AppVersion'] = str_replace("版本：","",$appdetailinfo[1]->plaintext);
	$appdetailinfo_arr['AppVersion'] = str_replace(" ","",$appdetailinfo_arr['AppVersion']);
	
	$appdetailinfo_arr['AppSize'] = str_replace("大小：","",$appdetailinfo[3]->plaintext);
	$appdetailinfo_arr['AppSize'] = str_replace(" ","",$appdetailinfo_arr['AppSize']);
	var_dump($app_detail_url);die();
	$html_detail->clear();
	return $appdetailinfo_arr;
}



function get_exit_appinfo_fromdb()
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'select id, packageName,title from jk_apps_info_anzhi where IsDeleted=0 '; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_info = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_info_temp = array();
  		$app_exit_info_temp['id'] = $row['id'];
	  	$app_exit_info_temp['AppPackageName'] = $row['packageName'];
	  	$app_exit_info_temp['AppName'] = $row['title'];
	  	$app_exit_info[] = $app_exit_info_temp;
  	}
	return $app_exit_info;
}

function get_exit_appversion_fromdb()
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'select id,app_id,versionName,versionCode from jk_apps_version_anzhi where IsDeleted=0 '; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_version = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_version_temp = array();
	  	$app_exit_version_temp['id'] = $row['id'];
	  	$app_exit_version_temp['app_id'] = $row['app_id'];
	  	$app_exit_version_temp['versionName'] = $row['versionName'];
	  	$app_exit_version_temp['versionCode'] = $row['versionCode'];
	  	$app_exit_version[] = $app_exit_version_temp;
  	}
	return $app_exit_version;
}


function get_today_data()
{
	$today = date("Y-m-d");
	$link = mysql_connect("localhost","root",DBPAS); 
	
	$sql = 'select id, app_id,rank,ranktype,time from jk_apps_data_mobile_anzhi_'.date('Ym',time()).' where IsDeleted=0 and time="'.$today.'"'; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_info = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_info_temp = array();
  		$app_exit_info_temp['id'] = $row['id'];
  		$app_exit_info_temp['app_id'] = $row['app_id'];
  		$app_exit_info_temp['rank'] = $row['rank'];
	  	$app_exit_info_temp['ranktype'] = $row['ranktype'];
	  	$app_exit_info_temp['time'] = $row['time'];
	  	$app_exit_info[] = $app_exit_info_temp;
  	}
	return $app_exit_info;
}
function get_today_score()
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$today = date("Y-m-d");

	$sql = 'select id, app_id from jk_apps_score_data_mobile_anzhi_'.date('Ym',time()).' where IsDeleted=0 and time="'.$today.'"'; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_info = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_info_temp = array();
  		$app_exit_info_temp['id'] = $row['id'];
	  	$app_exit_info_temp['app_id'] = $row['app_id'];
	  	$app_exit_info[] = $app_exit_info_temp;
  	}
	return $app_exit_info;
}




//app基础信息
function insert_appinfo_ifnotexit_todb($app_info)
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'insert into jk_apps_info_anzhi (title,packageName,url,icon,apptype,appbigtype)
	 values ("'.$app_info['name'].'","'.$app_info['packagename'].'","'.WEBURLHEADER.$app_info['packagename'].'","'.$app_info['AppLogo'].'","'.$app_info['AppType'].'","'.$app_info['AppBigType'].'")'; 

	$rs = mysql_query($sql, $link);
	//echo $sql;die();
	$filename_success = LOGPATHHEADER.'logs/anzhi_info_success.log';
	$filename_fail = LOGPATHHEADER.'logs/anzhi_info_fail.log';
	$filename_db_fail = LOGPATHHEADER.'logs/anzhi_info_db_fail.log';

	try
	{
		if(!$rs)
		{
			//记录错误，二次抓取用
			$info_str = $app_info["name"].$sql."Valid result!\r\n";
			$fh = fopen($filename_db_fail, "a");
			fwrite($fh, $info_str);
			fclose($fh);
			echo $info_str;
		}
		else
		{
			$info_str = $app_info["name"]."success!\r\n";
			$filename = $filename_success;
			$fh = fopen($filename, "a");
			fwrite($fh, $info_str);
			fclose($fh);
		}
	}
	catch(Exception $e)
	{
		
	}
	
	return mysql_insert_id();
}
//app version信息
function insert_appversion_ifnotexit_todb($app_info)
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'insert into jk_apps_version_anzhi (app_id,versionName,versionCode)
	 values ('.$app_info['app_id'].',"'.$app_info['AppVersion'].'","'.$app_info['Versioncode'].'")';

	$rs = mysql_query($sql, $link);
	//echo $sql;die();
	$filename_success = LOGPATHHEADER.'logs/anzhi_info_success.log';
	$filename_fail = LOGPATHHEADER.'logs/anzhi_info_fail.log';
	$filename_db_fail = LOGPATHHEADER.'logs/anzhi_info_db_fail.log';

	try
	{
		if(!$rs)
		{
			//记录错误，二次抓取用
			$info_str = $app_info["name"].$sql."Valid result!\r\n";
			$fh = fopen($filename_db_fail, "a");
			fwrite($fh, $info_str);
			fclose($fh);
			echo $info_str;
		}
		else
		{
			$info_str = $app_info["name"]."success!\r\n";
			$filename = $filename_success;
			$fh = fopen($filename, "a");
			fwrite($fh, $info_str);
			fclose($fh);
		}
	}
	catch(Exception $e)
	{
		
	}
	return mysql_insert_id();
}
//排名信息
function update_appinfo_todb($app_info)
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));
	
	$today = date("Y-m-d");
	
	$sql = 'insert into jk_apps_data_mobile_anzhi_'.date('Ym',time()).' (app_id,app_version_id,rank,ranktype,time)
	 values ('.$app_info['app_id'].','.$app_info['app_version_id'].','.$app_info['AppRanking'].','.$app_info['AppRankingType'].',"'.$today.'")'; 

//	echo $sql;die();
	$rs = mysql_query($sql, $link);
	
	$filename_success = LOGPATHHEADER.'logs/anzhi_success.log';
	$filename_fail = LOGPATHHEADER.'logs/anzhi_fail.log';
	$filename_db_fail = LOGPATHHEADER.'logs/anzhi_db_fail.log';

	try
	{
		if(!$rs)
		{
			//记录错误，二次抓取用
			$info_str = $app_info["name"].$sql."Valid result!\r\n";
			$fh = fopen($filename_db_fail, "a");
			fwrite($fh, $info_str);
			fclose($fh);
			echo $info_str;
		}
		else
		{
			$info_str = $app_info["name"]."success!\r\n";
			$filename = $filename_success;
			$fh = fopen($filename, "a");
			fwrite($fh, $info_str);
			fclose($fh);
		}
	}
	catch(Exception $e)
	{
		
	}
}

function getcommentcount($apppackagename)
{
	$catchcommentcounturl = "http://comment.anzhi.com/comment/comment!getCommentSummary.action?target=$apppackagename&pageSize=10&f=phoenix2&v=3.24.1&vc=3918&u=b19e8780415b44108c0c7f4014898b7b938fd64b&ch=wandoujia_wap&pageNum=0";
	
	@$json_string = file_get_html($catchcommentcounturl);
	$obj=json_decode($json_string);
	//echo $obj->data->total;die();
	return $obj->count;
}

//监控信息
function update_appscore_todb($app_info)
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));
	
	$today = date("Y-m-d");
	
	if(strstr($app_info['AppDownTimes'],"万"))
	{
		$app_info['AppDownTimes'] = str_replace("万","",$app_info['AppDownTimes']) * 10000;
	}
	if(strstr($app_info['AppDownTimes'],"亿"))
	{
		$app_info['AppDownTimes'] = str_replace("亿","",$app_info['AppDownTimes']) * 100000000;
	}
	if($app_info['Rating'] == "")
	{
		$app_info['Rating'] = 0;
	}
	
	$commentcount = getcommentcount($app_info['packagename']);
	if($commentcount == "")
	{
		$commentcount = 0;
	}
	
	$sql = 'insert into jk_apps_score_data_mobile_anzhi_'.date('Ym',time()).' (app_id,app_version_id,downloads,score,scoretype,comments,time)
	 values ('.$app_info['app_id'].','.$app_info['app_version_id'].','.$app_info['AppDownTimes'].','.$app_info['Rating'].',"'.$app_info['ScoreType'].'",'.$commentcount.',"'.$today.'")'; 

//	echo $sql;die();
	$rs = mysql_query($sql, $link);
	
	$filename_success = LOGPATHHEADER.'logs/anzhi_success.log';
	$filename_fail = LOGPATHHEADER.'logs/anzhi_fail.log';
	$filename_db_fail = LOGPATHHEADER.'logs/anzhi_db_fail.log';

	try
	{
		if(!$rs)
		{
			//记录错误，二次抓取用
			$info_str = $app_info["name"].$sql."Valid result!\r\n";
			$fh = fopen($filename_db_fail, "a");
			fwrite($fh, $info_str);
			fclose($fh);
			echo $info_str;
		}
		else
		{
			$info_str = $app_info["name"]."success!\r\n";
			$filename = $filename_success;
			$fh = fopen($filename, "a");
			fwrite($fh, $info_str);
			fclose($fh);
		}
	}
	catch(Exception $e)
	{
		
	}
}







function getcomment()
{
	//获取已存在app基础数据
	
	$app_exit_infos = get_exit_appinfo_fromdb();
	foreach ($app_exit_infos as $app_exit)
	{
		$apppackagename =  $app_exit['AppPackageName'];
		
		decodeurlandgetcomment($app_exit,"http://comment.wandoujia.com/comment/comment!getCommentSummary.action?target=$apppackagename&pageSize=10&f=phoenix2&v=3.24.1&vc=3918&u=b19e8780415b44108c0c7f4014898b7b938fd64b&ch=wandoujia_wap&pageNum=");
	}
}


function decodeurlandgetcomment($app_exit,$urlheader)
{
	$app_exit_infos = get_exit_appinfo_fromdb();
	$app_exit_versions = get_exit_appversion_fromdb();
	$app_exit_comment = get_exit_appcomment_fromdb();
	
	for($i=0;;$i++)
	{
		$apps_url_app_all_list = $urlheader.$i;
	    
		@$json_string = file_get_html($apps_url_app_all_list);
		$obj = json_decode($json_string);
		
		$appinfoarr = $obj->comments;
		
		//本次为空，结束
		if((count($appinfoarr) == 0) || ($appinfoarr == ""))
		{
			break;
		}
		
		//有内容，解析
		$app_info_arr_insertdb_arr = catchcommentdata($appinfoarr);
		
		
		//写数据库
		foreach ($app_info_arr_insertdb_arr as $app_info_arr_insertdb)
		{
			$app_info_arr_insertdb['app_id'] = $app_exit['id'];
			$app_info_arr_insertdb['app_version_id'] = 0;
			foreach ($app_exit_versions as $app_versions)
			{
				if($app_versions['app_id'] == $app_info_arr_insertdb['app_id'])
				{
					$app_info_arr_insertdb['app_version_id'] = $app_versions['id'];
					break;
				}
			}
			//判断是否需要更新信息库
			$searchcommentid = searchcommentisexit($app_info_arr_insertdb, $app_exit_comment);
			if($searchcommentid == 0)
			{
				update_appcomment_todb($app_info_arr_insertdb,0);
				//_ifnotexit_todb($app_info_arr_insertdb);
				
				//重新获取存在app信息，因为有了更新
				$app_exit_comment = get_exit_appcomment_fromdb();
			}
		}
		
	}
}

//排名分类，app大类，app信息
function catchcommentdata($appcommentarr)
{
	$app_info_insertdb_arr = array();
	
	foreach ($appcommentarr as $appcomment)
	{
		$app_info_insertdb = array();
		$appcomment_data = $appcomment;

		$app_info_insertdb['id'] = $appcomment_data->id;
		$app_info_insertdb['content'] = addslashes($appcomment_data->content);
		$app_info_insertdb['date'] = $appcomment_data->date;
		$app_info_insertdb['author_name'] = addslashes($appcomment_data->authorName);
		$app_info_insertdb['author_id'] = $appcomment_data->authorId;
		$app_info_insertdb_arr[] = $app_info_insertdb;
	}
	return $app_info_insertdb_arr;
}

//监控信息
function update_appcomment_todb($app_comment,$platform_id)
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));
	
	//$datano = date("Y-m-d",strtotime($app_comment["date"]));
	
	$sql = 'insert into jk_apps_comments_detail_anzhi (app_id,app_version_id,platform_id,author_id,author_name,content,time)
	 values ('.$app_comment['app_id'].','.$app_comment['app_version_id'].','.$platform_id.',"'.$app_comment['author_id'].'","'.$app_comment['author_name'].'","'.$app_comment['content'].'","'.$app_comment['date'].'")'; 

//	echo $sql;die();
	$rs = mysql_query($sql, $link);
	
	$filename_success = LOGPATHHEADER.'logs/anzhi_success.log';
	$filename_fail = LOGPATHHEADER.'logs/anzhi_fail.log';
	$filename_db_fail = LOGPATHHEADER.'logs/anzhi_db_fail.log';

	try
	{
		if(!$rs)
		{
			//记录错误，二次抓取用
			$info_str = $app_comment["app_id"].$sql."Valid result!\r\n";
			$fh = fopen($filename_db_fail, "a");
			fwrite($fh, $info_str);
			fclose($fh);
			echo $info_str;
		}
		else
		{
			$info_str = $app_comment["app_id"]."success!\r\n";
			$filename = $filename_success;
			$fh = fopen($filename, "a");
			fwrite($fh, $info_str);
			fclose($fh);
		}
	}
	catch(Exception $e)
	{
		
	}
}



function get_exit_appcomment_fromdb()
{
	$link = mysql_connect("localhost","root",DBPAS); 
	
	$sql = 'select id,app_id,app_version_id,author_id,content,time from jk_apps_comments_detail_anzhi where IsDeleted=0 '; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_comment = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_comment_temp = array();
	  	$app_exit_comment_temp['id'] = $row['id'];
	  	$app_exit_comment_temp['app_id'] = $row['app_id'];
	  	$app_exit_comment_temp['app_version_id'] = $row['app_version_id'];
	  	$app_exit_comment_temp['author_id'] = $row['author_id'];
	  	$app_exit_comment_temp['content'] = $row['content'];
	  	$app_exit_comment_temp['time'] = $row['time'];
	  	$app_exit_comment[] = $app_exit_comment_temp;
  	}
	return $app_exit_comment;
}

function searchcommentisexit($app_comment, $app_exit_comment)
{
	foreach ($app_exit_comment as $app_exit) 
	{
		if (($app_comment['app_id'] == $app_exit["app_id"]) && ($app_comment['app_version_id'] == $app_exit["app_version_id"]) && ($app_comment['author_id'] == $app_exit["author_id"]) && ($app_comment['content'] == $app_exit["content"]) && ($app_comment['date'] == $app_exit["time"])) 
		{
			return $app_exit['id'];
		}
	}
	return 0;
}


?>